//
//  BleViewController.m
//  MMWMiotLinkDemo
//
//  Created by 程杰 on 2020/12/30.
//  Copyright © 2020 HGG. All rights reserved.
//

#import "BleViewController.h"
#import <SystemConfiguration/CaptiveNetwork.h>

#import "UIView+DDKit.h"
#import "MBProgressHUD+WQTips.h"

@interface BleViewController ()

@property (weak, nonatomic) IBOutlet UILabel *wifiNameLb;
@property (weak, nonatomic) IBOutlet UITextField *passTf;
@property (weak, nonatomic) IBOutlet UITextField *overTimeTf;
@property (weak, nonatomic) IBOutlet UILabel *successLb;

@end

@implementation BleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.successLb.text = @"";
    self.wifiNameLb.text = [self getWifiInfo][@"SSID"];
    self.passTf.text = @"12345678";
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.passTf resignFirstResponder];
    [self.overTimeTf resignFirstResponder];

}

- (IBAction)btnClick:(UIButton *)sender {
    
    if (self.wifiNameLb.text.length <= 0 ||
        self.passTf.text.length <= 0) {
        [self.view dd_showMessageHUD:@"WiFi和密码不能为空"];
        return;
    }
    
    if (!self.model) {
        [self.view dd_showMessageHUD:@"外设也断开，请重新连接"];
        return;
    }
    
    sender.selected = !sender.selected;
    
    if (sender.selected) {
        [sender setBackgroundColor:[UIColor grayColor]];
        sender.enabled = NO;
    }
    
    [self.view dd_showMessageHUD:@"开始配网"];
    
    int time = [self.overTimeTf.text intValue];
    NSLog(@"time is:%d",time);
    
    [[MLBLEManager sharedManager] mlBleConnectCBPeripheralModel:self.model overTime:time];
    
    [[MLBLEManager sharedManager] mlBleSmartConfigWifiName:self.wifiNameLb.text passworld:self.passTf.text];
    
    __weak typeof(self)weakself = self;
    [[MLBLEManager sharedManager] mlBleConnectStateCallBack:^(MLBLEConnectState state) {
            
//    }];
//    [[MLBLEManager sharedManager] setConnectState:^(MLBLEConnectState state) {
       
        switch (state) {
            case MLBLEConnectStateNone:
                NSLog(@"未配网");
                [weakself.view dd_showMessageHUD:@"未配网"];
                break;
            case MLBLEConnectStateConnecting:
                NSLog(@"配网中");
                [weakself.view dd_showMessageHUD:@"配网中"];
                break;
            case MLBLEConnectStateApSuccess:
                NSLog(@"ap配网成功");
                [weakself.view dd_showMessageHUD:@"ap配网成功"];
                break;
            case MLBLEConnectStateSuccess:{
                NSLog(@"配网成功");
                [weakself.view dd_showMessageHUD:@"配网成功"];
                
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//                    [weakself showInfo];
                    
                    MLBLEModel *model = [[MLBLEManager sharedManager] getCurrentModel];
                   NSString *showInfo = [NSString stringWithFormat:@"配网成功：\ntype:7\nmac:%@\nkind:%@\nmodel:%@\nname:%@\ndeviceName--需要提前调用【- (void)isGetDeviceName:(BOOL)isGet】方法设置为YES<==>:%@",
                                           model.mac,model.mlKind,model.mlKind,model.name,model.deviceName];
                     NSLog(@"info is:%@",showInfo);
                    
                    [weakself showInfo];
                    
                });
               
                break;
            }
            case MLBLEConnectStateError:
                NSLog(@"配网失败");
                [weakself.view dd_showMessageHUD:@"配网失败,请检查密码是否正确,是否是2.4g网络"];
                break;
            case MLBLEConnectStateDisconnect:
                NSLog(@"外设断开连接");
//                [weakself.view dd_showMessageHUD:@"外设断开连接"];
        
                break;
            case MLBLEConnectStateConnectFail:
                NSLog(@"连接外设失败");
                [weakself.view dd_showMessageHUD:@"连接外设失败"];
        
                break;
            case MLBLEConnectStateOvertime:
                NSLog(@"连接超时");
                [weakself.view dd_showMessageHUD:@"连接超时，断开连接,请检查密码是否正确,是否是2.4g网络"];
        
                break;
            case MLBLEConnectStateSetupNetOk:
                NSLog(@"已配网成功，无需配网");
                [weakself.view dd_showMessageHUD:@"已配网成功，无需配网"];
                break;
            case MLBLEConnectStateUnKnow:
                NSLog(@"模块返回状态未识别");
                [weakself.view dd_showMessageHUD:@"模块返回状态未识别,请还原设备"];
                break;
            default:
                break;
        }
        
    }];
    
    // 切换wifi测试   返回 未配网 一直到超时
 
}

- (void)showInfo {
    
    NSString *showInfo = [NSString stringWithFormat:@"配网成功：\ntype:7\nmac:%@\nkind:%@\nmodel:%@\nname:%@\ndeviceName--需要提前调用【- (void)isGetDeviceName:(BOOL)isGet】方法设置为YES<==>:%@",
                          self.model.mac,self.model.mlKind,self.model.mlKind,self.model.name,self.model.deviceName];

    self.successLb.text = showInfo;
   

    

    NSLog(@"showinfo:%@",showInfo);
    
}


-(NSDictionary*)getWifiInfo{
    
    CFArrayRef wifiInterfaces = CNCopySupportedInterfaces();
    
    if (!wifiInterfaces) {
        return nil;
    }
    
    NSArray *interfaces = (__bridge NSArray *)wifiInterfaces;
    
    for (NSString *interfaceName in interfaces) {
        CFDictionaryRef dictRef = CNCopyCurrentNetworkInfo((__bridge CFStringRef)(interfaceName));
        
        if (dictRef) {
            NSDictionary *networkInfo = (__bridge NSDictionary *)dictRef;
            NSLog(@"network info -> %@", networkInfo);
            
            return networkInfo;
        }
    }
    
    return nil;
}

@end
